package diary

import (
	"context"
	"time"

	"go-weixin/app/diary/api/internal/svc"
	"go-weixin/app/diary/api/internal/types"
	"go-weixin/app/diary/rpc/pb/proto/diary"

	"github.com/zeromicro/go-zero/core/logx"
)

type DiaryDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDiaryDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DiaryDetailLogic {
	return &DiaryDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DiaryDetailLogic) DiaryDetail(req *types.DiaryDetailReq) (resp *types.DiaryDetailResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	res, err := l.svcCtx.DiaryRpc.DiaryDetail(ctx, &diary.DiaryDetailReq{
		Id: req.Id,
	})
	if err != nil {
		return nil, err
	}

	resp = new(types.DiaryDetailResp)
	resp.Diary = types.Diary{
		Id:       res.Diary.Id,
		UserId:   res.Diary.UserId,
		Title:    res.Diary.Title,
		Content:  res.Diary.Content,
		CreateAt: res.Diary.CreateAt,
		UpdateAt: res.Diary.UpdateAt,
		DeleteAt: res.Diary.DeleteAt,
	}
	return
}
